package driving;

import java.io.IOException;
import java.net.MalformedURLException;

import com.g2room.driving.util.IpUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;

public class GrapWebClick {

	public static WebClient getWebClient() {
		 //创建一个webclient
		WebClient wc = new WebClient();
		String ip = IpUtil.getIp();
		wc.addRequestHeader("X-Real-IP", ip);
		wc.addRequestHeader("x-forwarded-for", ip);
		wc.addRequestHeader("WL-Proxy-Client-IP", ip);
		////设置请求报文头里的User-Agent字段
      wc.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
      //wc.addRequestHeader和request.setAdditionalHeader功能应该是一样的。选择一个即可。
      //其他报文头字段可以根据需要添加
      wc.getCookieManager().setCookiesEnabled(false);//开启cookie管理
      wc.getOptions().setJavaScriptEnabled(true);//开启js解析。对于变态网页，这个是必须的
      wc.getOptions().setCssEnabled(true);//开启css解析。对于变态网页，这个是必须的。
      wc.getOptions().setThrowExceptionOnFailingStatusCode(false);
      wc.getOptions().setThrowExceptionOnScriptError(false);
      wc.getOptions().setTimeout(10000);
      
      //htmlunit 对css和javascript的支持不好，所以请关闭之
      wc.getOptions().setJavaScriptEnabled(false);
      wc.getOptions().setCssEnabled(false);
      return wc;
	}
	
	public static void main(String[] args) {
		WebClient wc = getWebClient();
		try {
			for(int i =0;  i<1000; i++) {
				Page page = wc.getPage("http://www.1nsh.com");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
